<template>
  <ul>
    <tree-li :parent="item.value" :treeObj="item" :selectKeys="selectKeys" v-for="item in data" :key="item.value" />
  </ul>
</template>
<script>
  import treeLi from './treeLi.vue'

  export default {
  components: {
    treeLi,
  },
  name: 'treeDetail',
  props: {
    treeData: {
      type: Array,
      default: () => [],
    },
    limit: {
      type: [Number,String],
      default: 0,
    },
  },
  data() {
    return {
      data: [],
      selectKeys: [],
    }
  },
  watch: {
    treeData(newData) {
      //解析所有的数据
      const _newData = newData.map((item) => {
        return this.setIntoData(item)
      })
      this.data = _newData
    },
  },
  methods: {
    //套入数据
    setIntoData(obj) {
      if (obj.children) {
        obj.children.map((item) => {
          this.setIntoData(item)
        })
      }
      return obj
    },
    parentSelect(val) {
      if (!this.$parent.parentSelect) {
        this.checkAllStatus(val)
      }
    },
    //集体校验
    checkAllStatus(val) {
      const tip = val[0]
      if (tip == 'select') {
        val[1].map((item) => {
          if (this.checkoutLi(this.selectKeys, item) < 0) {
            // this.selectKeys.push(item);
            this.selectKeys = [item];
          }
        })
        //新增
        this.$emit('onSelect', {
          selectKeys: this.selectKeys,
        })
      } else {
        val[1].map((item) => {
          const _index = this.checkoutLi(this.selectKeys, item);
          if (this.checkoutLi(this.selectKeys, item) >= 0) {
            this.selectKeys.splice(_index, 1)
          }
        })
        //减少
        this.$emit('deleteSelect', {
          deleteSelectKeys: val[1],
          selectKeys: this.selectKeys,
        })
      }
    },
    //校验
    checkoutLi(data, objData) {
      let _index = -1;
      for (let i = 0; i < data.length; i++) {
        if (data[i].value == objData.value) {
          _index = i
          break
        }
      }
      return _index
    },
    parentLayer(val) {
      if (!this.$parent.parentLayer) {
        this.$emit('onSelectLayer', val)
      }
    },
  },
  mounted() {
    //解析所有的数据
    const _newData = this.treeData.map((item) => {
      return this.setIntoData(item)
    })
    this.data = _newData
  },
}
</script>
<style lang="less" scoped>
ul {
  width: 100%;
  height: auto;
}
</style>